package cn.ac.jaas.isc.vegetable.pbms.controller;

import cn.ac.jaas.isc.vegetable.common.core.constant.CacheConstants;
import cn.ac.jaas.isc.vegetable.common.core.constant.PbmsCacheConstants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcManufacturer;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcManufacturerService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 厂商表
 *
 * @author 张高昌
 * @date 2022-05-14 17:41:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/arcmanufacturer")
@Api(value = "arcmanufacturer", tags = "厂商管理")
public class ArcManufacturerController {

    private final ArcManufacturerService arcManufacturerService;

    /**
     * 分页查询
     *
     * @param page            分页对象
     * @param arcManufacturer 厂商表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_view')")
    public R getArcManufacturerPage(Page page, ArcManufacturer arcManufacturer) {
        return R.ok(arcManufacturerService.getArcManufacturerPage(page, arcManufacturer));
    }


    /**
     * 通过id查询厂商表
     *
     * @param manufacturerId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{manufacturerId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_view')")
    public R getById(@PathVariable("manufacturerId") Integer manufacturerId) {
        return R.ok(arcManufacturerService.getById(manufacturerId));
    }

    /**
     * 新增厂商表
     *
     * @param arcManufacturer 厂商表
     * @return R
     */
    @ApiOperation(value = "新增厂商表", notes = "新增厂商表")
    @SysLog("新增厂商表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_add')")
    @CacheEvict(value = PbmsCacheConstants.MANUFACTURER_DETAILS, allEntries = true)
    public R save(@RequestBody @Valid ArcManufacturer arcManufacturer) {
        return arcManufacturerService.saveManufacturer(arcManufacturer);
    }

    /**
     * 修改厂商表
     *
     * @param arcManufacturer 厂商表
     * @return R
     */
    @ApiOperation(value = "修改厂商表", notes = "修改厂商表")
    @SysLog("修改厂商表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_edit')")
    @CacheEvict(value = PbmsCacheConstants.MANUFACTURER_DETAILS, allEntries = true)
    public R updateById(@RequestBody @Valid ArcManufacturer arcManufacturer) {
        if (arcManufacturer.getManufacturerId() == null) {
            return R.failed("厂商id不能为空！");
        }
        return arcManufacturerService.updateManufacturerById(arcManufacturer);
    }

    /**
     * 通过id删除厂商表
     *
     * @param manufacturerId id
     * @return R
     */
    @ApiOperation(value = "通过id删除厂商表", notes = "通过id删除厂商表")
    @SysLog("通过id删除厂商表")
    @DeleteMapping("/{manufacturerId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_del')")
    @CacheEvict(value = PbmsCacheConstants.MANUFACTURER_DETAILS, allEntries = true)
    public R removeById(@PathVariable Integer manufacturerId) {
        return arcManufacturerService.removeManufacturerById(manufacturerId);
    }


    /**
     * 导出excel 表格
     *
     * @param arcManufacturer 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_export')")
    public List<ArcManufacturer> export(ArcManufacturer arcManufacturer) {
        return arcManufacturerService.list(Wrappers.query(arcManufacturer));
    }

    @ApiOperation(value = "获取设备所有厂商", notes = "获取设备所有厂商")
    @GetMapping("/getEquipmentManufacturerAll")
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_view')")
    @Cacheable(value = PbmsCacheConstants.MANUFACTURER_DETAILS, key = "1", unless = "#result.data.isEmpty()")
    public R getEquipmentManufacturerAll() {
        return R.ok(arcManufacturerService.list((Wrappers.<ArcManufacturer>query().lambda().eq(ArcManufacturer::getType, 1))));
    }

    @ApiOperation(value = "获取装置所有厂商", notes = "获取装置所有厂商")
    @GetMapping("/getDeviceManufacturerAll")
    @PreAuthorize("@pms.hasPermission('pbms_arcmanufacturer_view')")
    @Cacheable(value = PbmsCacheConstants.MANUFACTURER_DETAILS, key = "2", unless = "#result.data.isEmpty()")
    public R getDeviceManufacturerAll() {
        return R.ok(arcManufacturerService.list((Wrappers.<ArcManufacturer>query().lambda().eq(ArcManufacturer::getType, 2))));
    }
}
